/**
 * @file qq_net.h
 *
 */
#ifndef QQ_NET_H
#define QQ_NET_H

#ifdef __cplusplus
extern "C" {
#endif

/*********************
 *      INCLUDES
 *********************/

#ifdef LV_CONF_INCLUDE_SIMPLE
#include "lvgl.h"
#include "lv_ex_conf.h"
#else
#include "../../../lvgl/lvgl.h"
#include "../../../lv_ex_conf.h"
#endif

#define WATCH_QQ_NET_TEST       0
#if USE_LV_WATCH_QQ_NET != 0
/*********************
 *      DEFINES
 *********************/
#define WATCH_QQ_NET_BACKGROUND 1

#define QQ_NET_DEBUG      1

#if QQ_NET_DEBUG
#define qq_net_printf printf
#else
#define qq_net_printf
#endif

#define WATCH_HTTP_HEADER1             "Accept: */*"
#define WATCH_HTTP_HEADER2             "User-Agent:RTOS"
#define WATCH_HTTP_HEADER3             "Connection:Keep-Alive"

#define WATCH_APPKEY_GET_QQ_TOKEN      "5e5ce2ff4cacddc1048e1eb94db9b5af"

#define  WATCH_QQ_APP_ID               "101975771"

#define  WATCH_QQ_APP_TYPE              (3)

#define  WATCH_QQ_GET_QR_URL            "https://graph.qq.com/oauth2.0/get_qr"

#define  WATCH_QQ_GET_TOKEN_URL         "https://graph.qq.com/oauth2.0/token"
#define  WATCH_QQ_GET_OPENID_URL        "https://graph.qq.com/oauth2.0/me"

#define WATCH_QQ_GET_USERINFO_URL       "https://graph.qq.com/user/get_user_info?access_token=%s&oauth_consumer_key=%s&openid=%s"

#define WATCH_QQ_HOST                   "https://opensdk.mobile.qq.com"
#define WATCH_QQ_OIDBIN_HOST            "https://oidb.tim.qq.com"

#define WATCH_QQ_BYTES_CLINET_VERSION   "QVNSIFFRIFYxLjA="

#define WATCH_QQ_STRING_ADD_FRIEND_MSG  "wLTX1MrWse1RUbLp1dLM7bzT"

#define  WATCH_QQ_ENCODE_REDIRECT_URL   "http://www.asrmicro.com/index.html"

#define WATCH_QQ_GUID_FOR_TEST          "9c8d6fab60d727eefdaef89507380a7a"

#define WATCH_QQ_LICENSE_FOR_TEST       "304602210096C89BE31AED0460D5F4F76A0B1579FA37C9AEF9191E1B37FBF8A89AA4DB9AD3022100D27B44B0F43B50730D6C6C31D0838713BBEAACBEB830A95C901BE86EE0C29FC0"

#define WATCH_QQ_SIGN_IDENTITY          "JianYouQQWatch"

#define WATCH_QQ_BACKGROUND_SERVER_URL  "http://api.new.iot08.com"//"http://api.device.static.iot08.com"

#define WATCH_QQ_UPLOAD_STATISTICS_APP_TYPE  120

/**********************
 *      TYPEDEFS
 **********************/
enum
{
    WATCH_QQ_NET_HOST = 0,
    WATCH_QQ_NET_OIDBIN_HOST,
}; //for build url

typedef struct
{
    char name[64];
}watch_qq_service_t;

enum {
    WATCH_QQ_GET_QR_REQ = 0,
    WATCH_QQ_QUERY_QR_REQ,
    WATCH_QQ_GET_ACCESS_TOKEN_REQ,
    WATCH_QQ_GET_OPENID_REQ,
    WATCH_QQ_REGISTER_WITH_OPENID_REQ,
    WATCH_QQ_SEND_MSG_REQ,
    WATCH_QQ_GET_MSG_REQ,
    WATCH_QQ_GET_FRIEND_MSG_REQ,
    WATCH_QQ_GET_FRIEND_LIST_REQ,
    WATCH_QQ_GET_FRIEND_INFO_REQ,
    WATCH_QQ_GET_RECENT_CONTACT_REQ,
    WATCH_QQ_FIND_FRIEND_REQ,
    WATCH_QQ_ADD_FRIEND_REQ,
    WATCH_QQ_ACK_ADD_FRIEND_REQ,
    WATCH_QQ_DELETE_FRIEND_REQ,
    WATCH_QQ_GET_UNDECIDED_REQ,
    WATCH_QQ_GET_DATA_REQ,
    WATCH_QQ_GET_LICENSE_GUID_REQ,
    WATCH_QQ_GET_OPENID_ACCESSTOKEN_REQ,
    WATCH_QQ_UPLOAD_STATISTICS_REQ,
};

typedef struct
{
    char *appid;
    char *sn;
    char *license;
    char *guid;
    char *query_url;
    char *code;
    char *access_token;
    char *openid;
    int  token_expires;
} watch_qq_net_mng_t;

typedef struct
{
    char *strdata;
    uint64_t msg_seqid;
    uint8_t retryflg;
} watch_qq_send_msg_buf_t;

/**********************
 * GLOBAL PROTOTYPES
 **********************/
static const watch_qq_service_t watch_qq_service[]= {
    {"/v3/openqq/watch_register"},
    {"/v3/openqq/push_notify"},
    {"/v3/openqq/sendmsg"},
    {"/v3/openqq/getmsg"},
    {"/v3/openqq/getfriendmsg"},
    {"/v3/openqq/getfriendlist"},
    {"/v3/openqq/audiotransfrom"},
    {"/v3/openqq/msgreaded"},
    {"/v3/openqq/evokeqq"},
    {"/v3/openqq/longpolling"},
    {"/v3/openqq/getfriendinfo"},
    {"/v3/openqq/getrecentcontact"},
    {"/v3/openqq/getdiscusslist"},
    {"/v3/openqq/getdiscussmsgseq"},
    {"/v3/openqq/getdiscussmsg"},
    {"/v3/openqq/senddiscussmsg"},
    {"/v3/openqq/gettmpmsg"},
    {"/v3/openqq/getgrouplist"},
    {"/v3/openqq/getgroupinfo"},
    {"/v3/openqq/getgroupmember"},
    {"/v3/openqq/getgroupmsgseq"},
    {"/v3/openqq/getgroupmsg"},
    {"/v3/openqq/groupmsgreaded"},
    {"/v3/openqq/setgroupmsgflag"},
    {"/v3/openqq/getgroupmsgflag"},
    {"/v3/openqq/sendgroupmsg"},
    {"/v3/oidbinterface/findfriend_0x72e_2"},
    {"/v3/oidbinterface/reqfriend_0x7c2_1"},
    {"/v3/oidbinterface/ackaddfriend_0xb5d_38"},
    {"/v3/oidbinterface/reqfriend_0xb58_20"},
    {"/v3/oidbinterface/requndecided_0x5cf_3"},
    {"/getway/qqlogin/watch/getbaseinfo"},
    {"/getway/qqlogin/watch/loginthird"},
    {"/v3/WirelessReg/uploadstatistics"},
};

extern int dm_base64_encode(const unsigned char *sourcedata, int source_size, char *base64);
extern int dm_base64_decode(const char *base64, unsigned char *dedata);

/**********************
 *      MACROS
 **********************/

#endif /*USE_LV_WATCH_QQ_NET*/

#ifdef __cplusplus
} /* extern "C" */
#endif

#endif /*QQ_NET_H*/
